#include "HotelItem.h"

HotelItem::HotelItem() 
{
	
}

HotelItem::HotelItem(string ID,string name,string add,
                     string country,string district,string cat)
{
    this->hotelID = ID;
    this->name = name;
    address = add;
    country = country;
    district = district;
    category = cat;
}

void HotelItem::setHotelID(string ID)
{
    this->hotelID = ID;
}

void HotelItem::setName(string name)
{
    this->name = name;
}

void HotelItem::setAddress(string add)
{
    this->address = add;
}

void HotelItem::setCountry(string country)
{
    this->country = country;
}

void HotelItem::setDistrict(string district)
{
    this->district = district;
}

void HotelItem::setCategory(string cat)
{
    this->category = cat;
}

string HotelItem::getHotelID()
{
    return this->hotelID;
}

string HotelItem::getName()
{
    return this->name;
}

string HotelItem::getAddress()
{
    return this->address;
}

string HotelItem::getCountry()
{
    return this->country;
}

string HotelItem::getDistrict()
{
    return this->district;
}

string HotelItem::getCategory()
{
    return this->category;
}

void HotelItem::addHotelItem()
{
    HotelItem createhotel;
    vector<HotelItem> toDB;
    dbHotelItem('s', tempVector);

    cout<<"Hotel Item To Add!!"<<endl;
    
    bool unique = false;
    
    while(!unique)
    {
        cin>>createhotel;
        
        vector<HotelItem>::iterator q = tempVector.begin();
        unique = true;
        
        while(q != tempVector.end() && unique)
        {           
            if((*q).hotelID == createhotel.hotelID)
                unique = false;
            
            ++q;
        }
        
        if(!unique)
            cout << endl << "Error: Hotel ID already exist! Re-Enter New Hotel Details!" << endl << endl;
    }
    
    if(confirmation())
    {
        toDB.push_back(createhotel);
        dbHotelItem('i', toDB);
    }

}
void HotelItem::deleteHotelItem()
{
    int count=0, counter = 0;
		tempVector.clear();
		dbHotelItem('s', tempVector);
		vector<HotelItem> vecToDB;
		
    p=tempVector.begin();
    
    while(p != tempVector.end()){
        
        cout<<"["<<count+1<<"] "<<(*p);
        count++;
        p++;
    }
    
    do
    {
        cout << "Which one to delete?";
        cin >> counter;
        cin.clear();
	cin.ignore(100, '\n');
        
        if (counter > count || counter < 0)
        {
            cout << "Invalid Option Please try again" << endl;
        }
    } while (counter > count || counter < 0);
    
    if(counter != 0) {
				p=tempVector.begin();
        p+= counter-1;
        
        vecToDB.push_back(*p);
				dbHotelItem('d', vecToDB);
    }
		
}

void HotelItem::modifyHotelItem()
{
    int count=0, counter = 0;
    int choice;
    string temp;
    
		tempVector.clear();
		dbHotelItem('s', tempVector);
		vector<HotelItem> vecToDB;		
    p=tempVector.begin();
    while(p != tempVector.end()){
        
        cout<<"["<<count+1<<"] "<<(*p);
        count++;
        p++;
    }
    
    do
    {
        cout<<"Which one to modify? (0 to return):";
        cin>>counter;
        cin.clear();
        cin.ignore(100,'\n');	
        if (counter > count || counter < 0)
        {
            cout << "Invalid Entry! Please try again" << endl;
        }
	
    } while (counter > count || counter < 0);
        
		
    if (counter != 0)
    {
        counter-=1;
    p=tempVector.begin();
    p += counter;
    
    cout<<
    "What do you want to modify?"<<endl<<
    "1)\tName"<<endl<<
    "2)\tAddress"<<endl<<
    "3)\tPlace Of Origin"<<endl<<
    "4)\tDistrict Of Origin"<<endl<<
    "5)\tCategory"<<endl;
    
    cout<<"Choice:";
    cin>>choice;
    cin.clear();
    cin.ignore(100,'\n');
    
    switch(choice){
            
        case 1:
        {
            cout<<"Please Enter New Name:";
            getline(cin, temp);
            
            if(confirmation()){
                
                cout<<(*p).getName()<<" -->  "<<temp<<endl;
                (*p).setName(temp);
            }
            break;
        }
        case 2:
        {
            
            cout<<"Please Enter New Address:";
            getline(cin, temp);
            
            if(confirmation()){
                
                cout<<(*p).getAddress()<<" -->  "<<temp<<endl;
                (*p).setAddress(temp);
            }
            break;
        }
        case 3:
        {
            cout<<"Please Enter New Place Of Origin:";
            getline(cin, temp);
            
            if(confirmation()){
                
                cout<<(*p).getCountry()<<" -->  "<<temp<<endl;
                (*p).setCountry(temp);
            }
            break;
        }
        case 4:
        {
            cout<<"Please Enter New District:";
            getline(cin, temp);
            
            if(confirmation()){
                
                cout<<(*p).getDistrict()<<" -->  "<<temp<<endl;
                (*p).setDistrict(temp);
            }
            break;
        }
        case 5:
        {
            cout<<"Please Enter New Category:";
            getline(cin, temp);
            
            if(confirmation()){
                
                cout<<(*p).getCategory()<<" -->  "<<temp<<endl;
                (*p).setCategory(temp);
            }
            break;
        }
        default:
        {
            cout<<"wrong input please re enter:";
            cin>>choice;
        }
    }
    
		vecToDB.push_back(*p);
		dbHotelItem('u', vecToDB);
}
        cout << "Returning to Main Menu" << endl;
}

void HotelItem::display(){
    
    p=tempVector.begin();
    
    while(p!= tempVector.end()){
        
        cout<<(*p);
        p++;
    }
}

bool HotelItem::confirmation(){
    
    char confirm;
    
    do
    {
	cout<<"Are you sure?(y/n):";
	cin>>confirm;
	cin.clear();
	cin.ignore(100, '\n');
		
    }while(!(confirm =='y' || confirm == 'n'));
    
    switch(confirm){
            
        case 'y':
        {
            return true;
        }
        case 'n':
        {
            return false;
        }
        default:
        {
            cout<<"Wrong input. Please re-enter:";
            cin>>confirm;
						cin.clear();
						cin.ignore(100, '\n');
        }
    }
    return false;
}

bool HotelItem::validHotelId(vector<HotelItem>& hotelId, string userHotelId)
{
    vector<HotelItem>::iterator p = hotelId.begin();
    bool correct = false;
    
    while(p != hotelId.end())
    {
        if(p->getHotelID() == userHotelId)
            return true;
        ++p;
    }
    
    return false;
}

ostream& operator<<(ostream& out, HotelItem& anitem){
    
    out<<anitem.getHotelID()<<","<<anitem.getName()<<","<<anitem.getAddress()<<","<<anitem.getCountry()<<","<<anitem.getDistrict()<<","<<anitem.getCountry()<<endl;
    
    return out;
}

istream& operator>>(istream& in, HotelItem& anitem){
    
    string eid;
    string nme;
    string addr;
    string contry;
    string distr;
    string catr;
    
    bool correct = false;
    
    while(!correct)
    {
        cout<<"Enter HotelID (Start with HIXXXXXX Eg. HI00001) :";
        getline(cin, eid);
        
        if(!(eid[0] == 'H' && eid[1] == 'I'))
            cout << "Error : In-correct Hotel Id! Re-";
        else
        {
            bool isNum = true;
            
            int i = 2;
            
            while(isNum && (i < eid.length()))
            {
                if(!(isdigit(eid[i])))
                    isNum = false;
                ++i;
            }
            
            if(!isNum)
                cout << "Error: In-correct Hotel Id (Found non-digit Values)! Re-";
            else
                correct = true;
        }
        
    }
    
    anitem.setHotelID(eid);
    
    cout<<"Enter Hotel Name:";
    getline(cin, nme);
    anitem.setName(nme);
    
    cout<<"Enter Hotel Address:";
    getline(cin,addr);
    anitem.setAddress(addr);
    
    cout<<"Enter Country of Origin:";
    getline(cin,contry);
    anitem.setCountry(contry);
    
    cout<<"Enter District of Origin:";
    getline(cin,distr);
    anitem.setDistrict(distr);
    
    cout<<"Enter Category:";
    getline(cin,catr);
    anitem.setCategory(catr);
    
    return in;
}